package com.isunland.managesystem.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.utils.MyUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class NetWorkSettingDialogFragment extends DialogFragment {
  private EditText etEditHost;
  private AlertDialog mNetworkDialog;

  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_network_setting, null);
    EditText etCurrentHost = (EditText) v.findViewById(R.id.et_CurrentHost);
    //开发者模式启用路径选择
    if (MyUtils.isDevelopMode(getActivity())) {
      etCurrentHost.requestFocus();
      etCurrentHost.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View view) {
          choosePath();
        }
      });
    }

    etCurrentHost.setText(ApiConst.sUrlMain);
    etEditHost = (EditText) v.findViewById(R.id.et_editHost);

    mNetworkDialog = new AlertDialog.Builder(getActivity()).setTitle(R.string.setting_network)
        .setView(v)
        .setPositiveButton(android.R.string.ok, null

        )
        .setNegativeButton(android.R.string.cancel, new OnClickListener() {

          @Override public void onClick(DialogInterface dialog, int which) {

          }
        })
        .create();

    mNetworkDialog.setOnShowListener(new DialogInterface.OnShowListener() {

      @Override public void onShow(DialogInterface dialog) {

        Button b = mNetworkDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

          @Override public void onClick(View view) {
            changePath();
          }
        });
      }
    });
    return mNetworkDialog;
  }

  private void choosePath() {

    ArrayList<String> nameArr = MyUtils.getServerPaths();
    if (nameArr == null || nameArr.size() == 0) return;
    Collections.sort(nameArr, new Comparator<String>() {
      public int compare(String mapping1, String mapping2) {
        return mapping1.compareTo(mapping2);
      }
    });
    AlertDialog.Builder builderSingle = new AlertDialog.Builder(getActivity());
    //builderSingle.setIcon(R.drawable.ic_launcher);
    builderSingle.setTitle("Select One Path:");

    final ArrayAdapter<String> arrayAdapter =
        new ArrayAdapter<>(getActivity(), android.R.layout.select_dialog_singlechoice);
    arrayAdapter.addAll(nameArr);

    builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
      @Override public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        mNetworkDialog.dismiss();
      }
    });

    builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
      @Override public void onClick(DialogInterface dialog, int which) {
        String strName = arrayAdapter.getItem(which);
        etEditHost.setText(strName);
        changePath();
      }
    });
    builderSingle.show();
  }

  private void changePath() {
    String newHost = etEditHost.getText().toString().trim();
    if (newHost.length() == 0) {

      Toast.makeText(getActivity(), "地址不能为空", Toast.LENGTH_SHORT).show();
      return;
    }
    String developHost = isDeveloper(newHost);
    if (developHost != null) {
      newHost = developHost;
    }
    if (!MyUtils.isUrl(newHost)) {
      Toast.makeText(getActivity(), "格式不正确:" + newHost, Toast.LENGTH_SHORT).show();
    } else {

      // mApplication.setUrlMain(newHost);
      ApiConst.setHost(newHost, getActivity().getApplicationContext());
      if (getTargetFragment() != null) {
        Intent i = new Intent();
        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
      }

      Toast.makeText(getActivity(), "服务器地址更改为:" + newHost, Toast.LENGTH_SHORT).show();
      mNetworkDialog.dismiss();
    }
  }

  private String isDeveloper(String newHost) {
    if (TextUtils.isEmpty(newHost)) {
      return null;
    }
    if (newHost.equalsIgnoreCase("#gx") || newHost.equalsIgnoreCase("#.gx")) {
      return ApiConst.HOST_GUOXIN;
    }
    if (newHost.equalsIgnoreCase("#wj") || newHost.equalsIgnoreCase("#.wj")) {
      return ApiConst.HOST_WEIJIA;
    }
    if (newHost.equalsIgnoreCase("#.7")) {
      return "http://192.168.1.7";
    }
    if (newHost.equalsIgnoreCase("#.8")) {
      return "http://192.168.1.8:8080";
    }

    if (newHost.contains("#.")) {
      return "http://192.168.2" + newHost.substring(1, newHost.length()) + ":8080/bpmx";
    }

    return null;
  }
}

